﻿Imports ControleMatricula.Negocio.Classes
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports ControleMatricula.Negocio

Public Class FrmRelContatos
    Dim db As New DataClassesDataContext()

    Private Sub FrmRelContatos_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        MontarComboEspecifica(cmbEspecifica)
        cmbEspecifica.SelectedIndex = -1

    End Sub

    Private Sub btnReceber_Click(sender As System.Object, e As System.EventArgs) Handles btnReceber.Click

        Dim ListEspecifica As List(Of SPEspecificaConsultarResult)
        ListEspecifica = db.SPEspecificaConsultar(0, Convert.ToString(cmbEspecifica.Text)).ToList()

        Dim Especifica As Integer = 0

        If cmbEspecifica.Text <> String.Empty Then
            Especifica = cmbEspecifica.SelectedValue
        End If

        Dim ListContatos As List(Of SPRelContatosResult)
        ListContatos = db.SPRelContatos(CDate(DTInicial.Text), CDate(DTFinal.Text), Especifica).ToList()

        Dim DSContatos As DataSet
        DSContatos = New DataSet()

        DSContatos.Tables.Add(New DataTable("SPRelContatos"))

        DSContatos.Tables(0).Columns.Add("Matricula")
        DSContatos.Tables(0).Columns.Add("Nome")
        DSContatos.Tables(0).Columns.Add("DDDTelefone")
        DSContatos.Tables(0).Columns.Add("Telefone")
        DSContatos.Tables(0).Columns.Add("DDDCelular")
        DSContatos.Tables(0).Columns.Add("Celular")
        DSContatos.Tables(0).Columns.Add("Email")
        DSContatos.Tables(0).Columns.Add("DEEspecifica")
        DSContatos.Tables(0).Columns.Add("DataInicio")
        DSContatos.Tables(0).Columns.Add("DataFim")

        If ListContatos.Count > 0 Then
            For x = 0 To ListContatos.Count - 1
                Dim row As DataRow = DSContatos.Tables(0).NewRow()

                row("Matricula") = Mid(ListContatos(x).Matricula, 1, 1) & "/" & Mid(ListContatos(x).Matricula, 2, 4) & "-" & Mid(ListContatos(x).Matricula, 6, 5)
                row("Nome") = ListContatos(x).Nome
                row("DDDTelefone") = ListContatos(x).DDDTelefone
                row("Telefone") = ListContatos(x).Telefone
                row("DDDCelular") = ListContatos(x).DDDCelular
                row("Celular") = ListContatos(x).Celular
                row("Email") = ListContatos(x).Email
                row("DEEspecifica") = IIf(Especifica = 0, "Todos", ListContatos(x).DEEspecifica)
                row("DataInicio") = ListContatos(x).DataInicio
                row("DataFim") = ListContatos(x).DataFim

                DSContatos.Tables(0).Rows.Add(row)
            Next
        Else
            MsgBox("Não foi encontrado nenhum registro!", MsgBoxStyle.Information)
            Exit Sub
        End If

        DSContatos.AcceptChanges()

        Dim DS As New DataSet

        DS.Tables.Add(DSContatos.Tables(0).Copy)

        DS.AcceptChanges()

        Dim Contatos As Contatos
        Contatos = New Contatos()

        Contatos.SetDataSource(DS)

        RelContatos.CrystalReportRelContatos.ReportSource = Contatos
        RelContatos.CrystalReportRelContatos.RefreshReport()

        RelContatos.Show()

    End Sub
End Class